package cn.shop.ordersystem.controller;

import cn.shop.ordersystem.mapper.ProductMapper;
import cn.shop.ordersystem.pojo.dto.ProductDTO;
import cn.shop.ordersystem.pojo.entity.Product;
import cn.shop.ordersystem.pojo.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;
    @RequestMapping("/product/insert")
    public void insert(@RequestBody ProductDTO productDTO){
        System.out.println("productDTO = " + productDTO);
        Product p=new Product();
        BeanUtils.copyProperties(productDTO,p);
        p.setSaleCount(0);
        System.out.println(p);
        mapper.insert(p);
    }
    @RequestMapping("/product/select")
    public List<ProductVO> select(){
        return mapper.select();
    }

    @RequestMapping("/product/selectById/update")
    public ProductVO selectByIdUpdate(int id,Product product){
        return mapper.selectById(id);
    }

    @RequestMapping("/product/delete")
    public void deleteById(int id){
        mapper.deleteById(id);
    }
    @RequestMapping("/product/update")
    public void update(@RequestBody ProductDTO productDTO){
        mapper.update(productDTO);
    }
}
